package com.example.demo.componet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;


@Configuration
//@PropertySource({"classpath:redis.properties","classpath:file.properties"})
@PropertySource("classpath:redis.properties")
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;


//    @Value("${filepath}")
//    private String filepath;


    /**
     * @Params
     * @Return @return {@link JedisConnectionFactory }
     * @Description 连接工厂
     * @Throws
     * @Author xwh
     * @Date 2023/01/25
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(){
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setPassword(password);

        JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfigurationBuilder = (JedisClientConfiguration.JedisClientConfigurationBuilder) JedisClientConfiguration.builder();

        JedisClientConfiguration jedisClientConfiguration = jedisClientConfigurationBuilder.build();
        return new JedisConnectionFactory(redisStandaloneConfiguration,jedisClientConfiguration);
    }

    /**
     * @Params @param redisConnectionFactory 复述,连接工厂
     * @Return @return {@link RedisTemplate }
     * @Description redis模板
     * @Throws
     * @Author xwh
     * @Date 2023/01/25
     */
    @Bean
    public RedisTemplate makeRedisTemplate(@Autowired RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        //redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

//    @Bean(name = "getFilePath")
//    public String getFilePath() throws UnsupportedEncodingException {
//        return new String(this.filepath.getBytes("ISO-8859-1"),"UTF-8");
//    }
}
